const { router, fs, urlencodedParser } = require('../CustomMethod');
const database = require('../CustomMethod/mysqlMethod');

router.post('/saveComment', urlencodedParser, async (req, res) => {
    // 将数据存入数据库
    let { paramValue, sourceModule, date, review_content } = req.body;
    //将数据存入数据库
    database('INSERT INTO user_reviews (review_id, review_type,review_content, review_time) VALUES (?,?,?,?)', [paramValue, sourceModule, review_content, date], function (err, result) {
        if (err) {
            res.send({ code: 0, module: "保存失败" });
            return;
        }
        //查询用户信息
        database('SELECT avatar, name FROM userinformation WHERE id =?', [paramValue], function (err, result) {
            if (err) {
                res.send({ code: 0, module: "保存失败" });
                return;
            }
            res.send({ code: 200, module: result });
        })
    })
});

router.post('/getReview', urlencodedParser, async (req, res) => {
    // 获取评论和用户信息
    let { sourceModule } = req.body;
    const query = `
        SELECT uf.avatar, uf.name, ur.review_type, ur.review_content, ur.review_time 
FROM userinformation uf 
LEFT JOIN user_reviews ur ON uf.id = ur.review_id 
WHERE ur.review_type = ?
    `;
    database(query, [sourceModule], function (err, result) {
        if (err) {
            res.send({ code: 0, module: "获取失败" });
            return;
        }
        res.send({ code: 200, module: result });
    })
})

module.exports = router;
